package com.imooc.Interceptor;

import com.imooc.base.BaseInfoProperties;

import com.imooc.exceptions.GraceException;
import com.imooc.grace.result.ResponseStatusEnum;
import com.imooc.utils.IPUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@Slf4j
public class PassportInterceptor extends BaseInfoProperties implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String userIP = IPUtil.getRequestIp(request);
        boolean isExist = redis.keyIsExist(MOBILE_SMSCODE + ":" + userIP);
        if (isExist){
            log.info("短信获取频率过高");
            GraceException.display(ResponseStatusEnum.SMS_NEED_WAIT_ERROR);
           return  false;
        }
        return  true;
    }
}
